package cn.wzy.controller;

import cn.hutool.core.util.StrUtil;
import cn.wzy.common.Constant;
import cn.wzy.common.Result;
import cn.wzy.pojo.User;
import cn.wzy.service.CustomerService;
import cn.wzy.service.impl.CustomerServiceImpl;
import cn.wzy.utils.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author nihaowzy
 * @date 2020/12/24 11:59
 */
@WebServlet("/customer.do")
public class CustomerServlet extends HttpServlet {

    CustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            String tag = req.getParameter("service");
            Method method = this.getClass().getDeclaredMethod(tag, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 根据条件查询客户请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void customerList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String,String> params = new ConcurrentHashMap<>(10);

        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        String deleted = req.getParameter("deleted");
        String userId = req.getParameter("userId");
        if (StrUtil.isNotBlank(name)){
            params.put("name",name);
        }
        if (StrUtil.isNotBlank(sex)){
            params.put("sex",sex);
        }
        if (StrUtil.isNotBlank(phone)){
            params.put("phone",phone);
        }
        if (StrUtil.isNotBlank(company)){
            params.put("company",company);
        }
        if (StrUtil.isNotBlank(userId)){
            params.put("userId",userId);
        }
        if (StrUtil.isNotBlank(deleted)){
            params.put("deleted",deleted);
        }
        Result result = customerService.queryPage(params, Integer.parseInt(page), Integer.parseInt(limit));

        RespUtil.write(resp,result);
    }

    /**
     * 添加客户请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void addCustomer(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        String phone = req.getParameter("phone");
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        Result result = customerService.addCustomer(name, phone, Integer.parseInt(sex), company, user.getId());

        RespUtil.write(resp,result);

    }

    /**
     * 修改客户请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void updateCustomer(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String company = req.getParameter("company");
        String phone = req.getParameter("phone");
        User user = (User) req.getSession().getAttribute(Constant.CURRENT_USER_KEY);
        Integer id = user.getId();

        Result result = customerService.updateCustomer(name, phone, Integer.parseInt(sex), company, id);
        RespUtil.write(resp,result);

    }

    /**
     * 修改客户状态请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void deletedCustomer(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");

        Result result = customerService.deleteCustomer(Integer.parseInt(id));
        RespUtil.write(resp,result);

    }

    /**
     * 批量修改客户业务员请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void batchSalesMan(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //业务员id
        String userId = req.getParameter("userId");
        //客户id
        String[] cIds = req.getParameterValues("cId");
        //批量修改客户的业务员id
        Result result = customerService.batchSalesMan(userId, cIds);
        RespUtil.write(resp,result);


    }

    /**
     * 新增拜访请求
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void addVisitLog(HttpServletRequest req, HttpServletResponse resp) throws Exception {
       //客户id
        String customerId = req.getParameter("id");

        //客户姓名
        String customerName = req.getParameter("name");
        //拜访时间
        String visitTime = req.getParameter("visitTime");
        //拜访内容
        String descp = req.getParameter("descp");
        //获取当前登录用户的id和名字
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.CURRENT_USER_KEY);
        Integer userId = user.getId();
        String realname = user.getRealname();
        //新增业务员的拜访记录
        Result result = customerService.addVisitLog(customerId,customerName,userId,realname,visitTime,descp);

        RespUtil.write(resp,result);

    }



}
